import { getToken, setToken, removeToken } from 'admin/utils/auth'
import router, { resetRouter } from 'admin/router'
import api from 'admin/api'
import store from 'admin/store'

const state = {
    token: getToken(),
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
}

const mutations = {
    SET_TOKEN: (state, token) => {
        state.token = token
    },
    SET_INTRODUCTION: (state, introduction) => {
        state.introduction = introduction
    },
    SET_NAME: (state, name) => {
        state.name = name
    },
    SET_AVATAR: (state, avatar) => {
        state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
        state.roles = roles
    },
}

const actions = {
    // 用户登录
    async login({ commit }, userInfo) {
        const { code, data } = await api.user.login(userInfo)
        console.log("code",code)
        if (code === 200) {
            commit('SET_TOKEN', data)
            setToken('d44ecda0-31f')
            // setToken(data)
            // let token = 'd44ecda0-31f1-9017-91d7-69ceb5cda4a8'
            // commit('SET_TOKEN', token)
            // setToken(token)
            this.dispatch('permission/generateRoutes') //获取菜单权限
            return true
        }
        return false
        // switch (username) {
        //     case "admin":
        //         if (password.trim() === "yulaiadmin") {
        //             commit("SET_TOKEN", "admin-token");
        //             setToken("admin-token");
        //         } else {
        //             Vue.prototype.$message.error("密码错误");
        //         }

        //         break;
        //     case "tempadmin":
        //         if (password.trim() === "tempadmin") {
        //             commit("SET_TOKEN", "admin-token");
        //             setToken("admin-token");
        //         } else {
        //             Vue.prototype.$message.error("密码错误");
        //         }

        //         break;
        //     case "zhongshan":
        //         if (password.trim() === "zszx123456") {
        //             commit("SET_TOKEN", "zhongshan-token");
        //             setToken("zhongshan-token");
        //         } else {
        //             Vue.prototype.$message.error("密码错误");
        //         }
        //         break;
        //     case "yizhongh":
        //         if (password.trim() === "yizhongh") {
        //             commit("SET_TOKEN", "yizhongh-token");
        //             setToken("yizhongh-token");
        //         } else {
        //             Vue.prototype.$message.error("密码错误");
        //         }
        //         break;
        //     case "hywuzhong":
        //         if (password.trim() === "hywuzhong") {
        //             commit("SET_TOKEN", "hywuzhong-token");
        //             setToken("hywuzhong-token");
        //         } else {
        //             Vue.prototype.$message.error("密码错误");
        //         }
        //         break;
        // }
    },
    // 获取用户信息
    async getInfo({ commit, state }) {
        const data = {
            'maomao-token': {
                roles: ['admin'],
                introduction: 'I am a super administrator',
                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                name: 'Super Admin',
            },
            'admin-token': {
                roles: ['admin'],
                introduction: 'I am a super administrator',
                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                name: 'Super Admin',
            },
            'editor-token': {
                roles: ['editor'],
                introduction: 'I am an editor',
                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                name: 'Normal Editor',
            },
            'zhongshan-token': {
                roles: ['zhongshan'],
                introduction: 'I am an editor',
                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                name: 'zhongshan',
            },
            'yizhongh-token': {
                roles: ['yizhongh'],
                introduction: 'I am an editor',
                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                name: 'yizhongh',
            },
            'hywuzhong-token': {
                roles: ['hywuzhong'],
                introduction: 'I am an editor',
                avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
                name: 'hywuzhong',
            },
        }['maomao-token']

        if (!data) {
            return Promise.reject('Verification failed, please Login again.')
        }
        const { roles, name, avatar, introduction } = data

        // roles must be a non-empty array
        if (!roles || roles.length <= 0) {
            return Promise.reject('getInfo: roles must be a non-null array!')
        }

        commit('SET_ROLES', roles)
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_INTRODUCTION', introduction)
        return data
    },

    // 用户退出登录
    logout({ commit, state, dispatch }) {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        this.commit('team/REMOVE_CURRENTEAM') //移除学校列表
        this.commit('team/REMOVE_TEAMS')
        removeToken()
        resetRouter()

        // reset visited views and cached views
        // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
        dispatch('tagsView/delAllViews', null, { root: true })

        setTimeout(() => {
            router.push('/login')
        }, 300)
    },

    // 移除token
    resetToken({ commit }) {
        return new Promise((resolve) => {
            commit('SET_TOKEN', '')
            commit('SET_ROLES', [])
            removeToken()
            resolve()
        })
    },

    // 动态修改权限
    async changeRoles({ commit, dispatch }, role) {
        const token = role + '-token'
        commit('SET_TOKEN', token)
        setToken(token)

        const { roles } = await dispatch('getInfo')

        resetRouter()

        // generate accessible routes map based on roles
        const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
        // dynamically add accessible routes
        router.addRoutes(accessRoutes)

        // reset visited views and cached views
        dispatch('tagsView/delAllViews', null, { root: true })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions,
}
